package cn.ljl.sys.controller;

import cn.ljl.basic.util.AjaxResult;
import cn.ljl.basic.util.PageList;
import cn.ljl.sys.domain.DictionaryType;
import cn.ljl.sys.query.DictionaryTypeQuery;
import cn.ljl.sys.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 接口类：数据字典类型
 */
@Api(value = "数据字典类型接口类",description = "处理数据字典类型的CRUD操作的")
@RestController //前端分离项目都是同时异步请求 - 异步请求都需要加上@ResponseBody
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //前端：get http://localhost:8080/dictionaryType/1
    @ApiOperation(value = "接口：查询单个对象")
    @GetMapping("/{id}")
    public DictionaryType findOne(@PathVariable("id")Long id){
        return dictionaryTypeService.loadById(id);
    }

    /**
     * 接口：分页和高级查询
     * @param dictionaryTypeQuery
     * @return
     */
    //前端 post http://localhost:8080/dictionaryType json对象{}
    @ApiOperation(value = "接口：分页和高级查询")
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    /**
     * 接口：查询所有
     * @return
     */
    //前端：get http://localhost:8080/dictionaryType
    @ApiOperation(value = "接口：查询所有")
    @GetMapping
    public List<DictionaryType> findAll(){
        return dictionaryTypeService.loadAll();
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/dictionaryType/1
    @ApiOperation(value = "接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            dictionaryTypeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 添加或修改
     * @param dictionaryType
     * @return
     */
    //前端：put http://localhost:8080/dictionaryType/ json对象{}
    @ApiOperation(value = "接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId()==null){
                dictionaryTypeService.save(dictionaryType);
            }else{
                dictionaryTypeService.update(dictionaryType);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    @ApiOperation(value = "接口：批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            //1.调用service批量删除
            dictionaryTypeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            //return new AjaxResult(false,"删除失败");
            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
}
